package de.dopichaj.labrador.inex;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.xml.resolver.tools.CatalogResolver;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import de.dopichaj.labrador.search.hit.DocHitMap;
import de.dopichaj.labrador.search.hit.MemoryHit;


/**
 * A class for reading INEX submissions into a DocHitMap.
 * Note that this class does not check the input for validity.
 */
public final class SubmissionReader {

    
    private static final class SubmissionHandler extends DefaultHandler {
        private File file;
        private String xPath;
        private float score;

        private StringBuilder content = new StringBuilder();
        
        private final DocHitMap docHitMap;
        
        public SubmissionHandler(DocHitMap docHitMap) {
            this.docHitMap = docHitMap;
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            content = new StringBuilder();
        }

        @Override
        public void endElement(String uri, String localName, String qName) {
            if (qName.equals("file")) {
                file = new File(content.toString());
            } else if (qName.equals("path")) {
                xPath = content.toString();
            } else if (qName.equals("rsv")) {
                score = Float.parseFloat(content.toString());
            } else if (qName.equals("result")) {
                docHitMap.addHit(new MemoryHit(file, xPath, 0, 0, score));
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) {
            content.append(new String(ch, start, length));
        }
    }
    
    public static DocHitMap readInexSubmission(InputStream input) throws ParserConfigurationException, SAXException, IOException {
        
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setEntityResolver(new CatalogResolver());
        
        DocHitMap result = new DocHitMap();
        xmlReader.setContentHandler(new SubmissionHandler(result));
        xmlReader.parse(new InputSource(input));
        
        return result;
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/